﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BOMS.DAL;

namespace BOMS.WebUI
{
    public partial class sysAddUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string userId = Request.QueryString["userId"].ToString();
            if (!Page.IsPostBack)
            {
                if (userId == "0")//添加
                {
                    bindRole();
                }
                else//编辑
                {
                    showUserInfo(userId);
                }
            }

        }

        public void showUserInfo(String userId)
        {
            DataTable dt = new DataTable();
            dt = SysAdmin.getUserInfoById(userId);
            this.userName.Text = dt.Rows[0]["userName"].ToString();
            this.name.Text = dt.Rows[0]["name"].ToString();

            DataTable dtRole = new DataTable();
            dtRole = SysAdmin.getAllRole();
            this.RoleList.DataSource = dtRole;
            this.RoleList.DataTextField = "role";
            this.RoleList.DataValueField = "id";
            this.RoleList.DataBind();

            for (int i = 0; i < RoleList.Items.Count; i++)
            {
                if (RoleList.Items[i].Value == dt.Rows[0]["roleId"].ToString())
                {
                    RoleList.Items[i].Selected=true;
                }
            }

        }

        public void bindRole()
        {
            DataTable dt = new DataTable();
            dt = SysAdmin.getAllRole();
            this.RoleList.DataSource = dt;
            this.RoleList.DataTextField = "role";
            this.RoleList.DataValueField = "id";
            this.RoleList.SelectedIndex = 2;
            this.RoleList.DataBind();
        }

        protected void add_Click(object sender, EventArgs e)
        {

            string userId = Request.QueryString["userId"].ToString();
            Boolean IsSuccess = false;
                if (userId == "0")//添加
                {
                     IsSuccess = SysAdmin.addUser(this.name.Text, this.userName.Text, this.pwd.Text, this.RoleList.SelectedValue);

                }
                else//编辑
                {
                    IsSuccess = SysAdmin.editUser(userId,this.name.Text, this.userName.Text, this.pwd.Text, this.RoleList.SelectedValue);
 
                }
                 Type cstype = this.GetType();
            if (IsSuccess == true)
            {
                //ClientScript.RegisterStartupScript(cstype, "key", "<script>window.returnValue='success';window.opener=null;window.close(this);</script>");
                ClientScript.RegisterStartupScript(cstype, "key", "dialogReturn('true');",true);
               
            }
            else
            {
               // ClientScript.RegisterStartupScript(cstype, "key", "<script>window.returnValue='failed';window.opener=null;window.close(this);</script>");
                ClientScript.RegisterStartupScript(cstype, "key", "dialogReturn('false');",true);
            }
        }


    }
}